package com.hqd.ch03.v45.web.context.support;

import com.hqd.ch03.utils.ClassUtils;
import com.hqd.ch03.v45.core.env.MutablePropertySources;
import com.hqd.ch03.v45.core.env.PropertySource;
import com.hqd.ch03.v45.core.env.StandardEnvironment;
import com.hqd.ch03.v45.web.context.ConfigurableWebEnvironment;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

public class StandardServletEnvironment extends StandardEnvironment implements ConfigurableWebEnvironment {

    /**
     * Servlet context init parameters property source name: {@value}.
     */
    public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams";

    /**
     * Servlet config init parameters property source name: {@value}.
     */
    public static final String SERVLET_CONFIG_PROPERTY_SOURCE_NAME = "servletConfigInitParams";

    /**
     * JNDI property source name: {@value}.
     */
    public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";


    // Defensive reference to JNDI API for JDK 9+ (optional java.naming module)
    private static final boolean jndiPresent = ClassUtils.isPresent(
            "javax.naming.InitialContext", StandardServletEnvironment.class.getClassLoader());


    /**
     * Create a new {@code StandardServletEnvironment} instance.
     */
    public StandardServletEnvironment() {
    }

    /**
     * Create a new {@code StandardServletEnvironment} instance with a specific {@link MutablePropertySources} instance.
     *
     * @param propertySources property sources to use
     * @since 5.3.4
     */
    protected StandardServletEnvironment(MutablePropertySources propertySources) {
        super(propertySources);
    }


    @Override
    protected void customizePropertySources(MutablePropertySources propertySources) {
        propertySources.addLast(new PropertySource.StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
        propertySources.addLast(new PropertySource.StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
        super.customizePropertySources(propertySources);
    }

    @Override
    public void initPropertySources(ServletContext servletContext, ServletConfig servletConfig) {
        //  WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
    }

}

